package com.eeepay.eeepay_v2.util;

import android.graphics.Color;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.widget.Button;

import com.eeepay.v2_library.view.LabelEditText;

/**
 * Created by Ching on 2016/3/9.
 * 实现TextWatcher监听EditText工具类
 */
public class CheckUtils {

    private static EditChangeListener editChangeListener;

    public static void addEditChangeListener(Button btn, int clickDraw, int unclickDraw, LabelEditText... editTexts) {

        editChangeListener = new EditChangeListener(btn, clickDraw, unclickDraw, editTexts);

        for (int i = 0; i < editTexts.length; i++) {
            editTexts[i].getEditText().addTextChangedListener(editChangeListener);
        }

    }

    static class EditChangeListener implements TextWatcher {

        private Button btn;
        private LabelEditText[] editTexts;
        private int clickDraw, unclickDraw;
        private String[] inputStr;
        private boolean flag;

        public EditChangeListener(Button btn, int clickDraw, int unclickDraw, LabelEditText... editTexts) {
            this.btn = btn;
            this.clickDraw = clickDraw;
            this.unclickDraw = unclickDraw;
            this.editTexts = editTexts;
            inputStr = new String[editTexts.length];
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            flag = true;
            for (int i = 0; i < editTexts.length; i++) {
                inputStr[i] = editTexts[i].getEditText().getText().toString();
                if (TextUtils.isEmpty(inputStr[i])) {
                    flag = false;
                }
            }
            if (flag) {
//                btn.setEnabled(true);
//                btn.setBackgroundResource(clickDraw);
                btn.setEnabled(true);
                btn.setTextColor(Color.parseColor("#ffffff"));
                btn.getBackground().setAlpha(255);//0~255透明度值 ，0为完全透明，255为不透明
                btn.setBackgroundResource(clickDraw);
            } else {
//                btn.setEnabled(false);
//                btn.setBackgroundResource(unclickDraw);
                btn.setEnabled(false);
                btn.setBackgroundResource(unclickDraw);
                btn.getBackground().setAlpha(255);//0~255透明度值 ，0为完全透明，255为不透明
                btn.setTextColor(Color.parseColor("#89c0f5"));
            }
        }

    }

}
